/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.bsn.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;
import org.hutech09.jsale.bsn.ICustomerManager;
import org.hutech09.jsale.bsn.common.DataExistsException;
import org.hutech09.jsale.bsn.common.DataNotExistedException;
import org.hutech09.jsale.bsn.common.IGenericDao;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Customer;

/**
 *
 * @author Nam
 */
@Stateless
public class CustomerManager implements ICustomerManager {

    @Resource(name = "jsale-mail")
    private Session jsalemail;

    @EJB
    private IGenericDao dao;

    @Override
    public Customer create(Customer customer) throws ItemExistsException {
        try {
            return dao.create(customer);
        } catch (DataExistsException ex) {
            throw new ItemExistsException(customer);
        }
    }

    @Override
    public Customer update(Customer customer) throws ItemNotFoundException {
        try {
            return dao.update(customer);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(customer);
        }
    }

    @Override
    public Customer delete(Customer customer) throws ItemNotFoundException {
        try {
            return dao.delete(customer);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(customer);
        }
    }

    @Override
    public List<Customer> findPaging(int first, int pageSize, String orderBy, boolean desc, Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        return dao.findPaging(Customer.class, first, pageSize, orderBy, desc, myFilter);
    }

    @Override
    public long count(Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        return dao.count(Customer.class, myFilter);
    }

    @Override
    public Customer find(String rowKey) {
        return dao.find(Customer.class, rowKey);
    }

    private void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
        MimeMessage message = new MimeMessage(jsalemail);
        message.setSubject(subject);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
        message.setText(body);
        Transport.send(message);
    }

    @Override
    public void sendMyMail() {
        try {
            sendMail("tiensi1991@gmail.com", "Jsale Test", "Thử thôi à!");
        } catch (NamingException ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
